import { StatusBar } from './statusbar';
import { ToolBar } from './toolbar';
import { Display } from './display';
import { Mouse } from './mouse';
import { Keyboard } from './keyboard';
import { Communication } from './communication';

class KvmClient {
  public _language = null;
  public _display = null as any;
  public _mouse = null as any;
  public _keyboard = null as any;
  public _statusbar = null as any;
  public _toolbar = null as any;
  public _communication = null as any;
  public scope;
  public showMessage;
  public PSKEY = 44;
  private _keydownHandler: (e: KeyboardEvent) => void;
  private _keyupHandler: (e: KeyboardEvent) => void;
  private _clickHandler: (e: MouseEvent) => void;
  constructor(scope: any, showMessage: any) {
    this.scope = scope;
    this.showMessage = showMessage;
    document.oncontextmenu = (e) => {
      return false;
    };
    this._keydownHandler = (e) => {
      this._toolbar.setDefineKey(e);
      // tslint:disable-next-line: deprecation
      const keyCode = e.keyCode || e.which || e.charCode;
      if (keyCode !== 0x7b) {
        if (e?.preventDefault) {
          e.preventDefault();
        } else {
          // tslint:disable-next-line: deprecation
          (window.event as Event).returnValue = false;
        }
      }
    };
    this._keyupHandler = (e) => {
      if (e && e.keyCode === this.PSKEY) {
        this._toolbar.setDefineKey(e);
      }
      // tslint:disable-next-line: deprecation
      const keyCode = e.keyCode || e.which || e.charCode;
      if (keyCode !== 0x7b) {
        if (e?.preventDefault) {
          e.preventDefault();
        } else {
          // tslint:disable-next-line: deprecation
          (window.event as Event).returnValue = false;
        }
      }
    };
    this._clickHandler = (e) => {
      this._toolbar.setDefineKey(e);
    };
    document.addEventListener('keydown', this._keydownHandler);
    document.addEventListener('keyup', this._keyupHandler);
    document.addEventListener('click', this._clickHandler);
  }

  run(
    language: any,
    ip: string,
    kvmPort: number,
    vmmPort: number,
    privilege: any,
    title: string,
    token: string,
    sessionId: string
  ): void {
    this._language = language;
    this._statusbar = new StatusBar(this, title);
    this._toolbar = new ToolBar(this, ip, vmmPort, privilege, language);
    this._display = new Display(this);
    this._mouse = new Mouse(this);
    this._keyboard = new Keyboard(this);
    this._communication = new Communication(this, ip, token, kvmPort, sessionId);
  }

  destroy(): void {
    if (this._keydownHandler) {
      document.removeEventListener('keydown', this._keydownHandler);
      this._keydownHandler = null!;
    }
    if (this._keyupHandler) {
      document.removeEventListener('keyup', this._keyupHandler);
      this._keyupHandler = null!;
    }
    if (this._clickHandler) {
      document.removeEventListener('click', this._clickHandler);
      this._clickHandler = null!;
    }
    if (this._communication) {
      this._communication.destroy();
      this._communication = null;
    }
  }
}
export function openKvm(scope: any, showMessage: any): KvmClient {
  const client = new KvmClient(scope, showMessage);
  const kvmParams = scope.kvmParams;
  client.run(
    kvmParams.local,
    kvmParams.IPA,
    kvmParams.port,
    kvmParams.vmmPort,
    kvmParams.privilege,
    kvmParams.title,
    kvmParams.token,
    kvmParams.sessionId
  );
  return client;
}
